﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities.IRepository;
using BLToolkit.Data;
using BLToolkit.DataAccess;
using Entities;
using BLToolkit.Data.Linq;
using Entities.ThuQuy;

namespace DAL.ThuQuy
{
    public class BLTHoaDonRepository : ConnectionStringBL,IHoaDonRepository
    {
        //private DbManager db = new DbManager("QuanLiHoaDon");
        public bool ThemHD(HoaDon hd)
        {
            try
            {
                int kq = db.Insert(hd);
                
                db.CommitTransaction();
                return kq > 0;
            }
            catch (Exception)
            {

                db.RollbackTransaction();
                return false;
            }
        }

        public bool XoaHD(HoaDon hd)
        {
            try
            {
                int kq = db.Delete(hd);
                db.CommitTransaction();
                return kq > 0;
            }
            catch (Exception)
            {
                db.RollbackTransaction();
                return false;
            }
        }

        public bool CapNhatHD(HoaDon hd)
        {
            try
            {
                int kq = db.Update(hd);
                db.CommitTransaction();
                return kq > 0;
            }
            catch (Exception)
            {

                db.RollbackTransaction();
                return false;
            }
        }

        public HoaDon LayTheoID(string Id)
        {
            return db.GetTable<HoaDon>().SingleOrDefault(x => x.MaHD == Id);
        }

        public HoaDon XemChiTietTheoID(string Id)
        {
            HoaDon hd = LayTheoID(Id);
            if (hd != null)
            {
                hd.hoaDon = db.GetTable<HoaDon>().SingleOrDefault(x => x.MaHD == hd.MaHD);
            }
            return hd;
        }

        public List<HoaDon> LayTatCa()
        {
            return db.GetTable<HoaDon>().ToList();
        }
    }
}
